package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;

import com.xhui.crm.common.exception.RtManagerException;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpluscom.xhui.vimarket.zkplusxt;
import com.xhui.vimarket.zkplus.zulcom.xhui.vimarket.zkplusxecListener;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrInst;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrTmpl;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.impl.AttrTmplImpl;
import com.xhui.vimarket.zkplus.com.xhui.vimarket.zkplustrCompFactory;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTreeTabComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrTabboxComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrTabboxComp inst   = new AttrTabboxComp();
    
    /**
     * 日志.
     */
    private static Logger         logger = Logger.getLogger(AttrTabboxComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrTabboxComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrTabboxComp instance() {
        return AttrTabboxComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // tabbox
        final Tabbox tabbox = new Tabbox();
        tabbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        tabbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        // tabs
        final Tabs tabs = new Tabs();
        // tabpanels
        final Tabpanels tabpanels = new Tabpanels();
        
        // add to zul
        tabbox.setParent(comp);
        tabs.setParent(tabbox);
        tabpanels.setParent(tabbox);
        
        // comp log
        if (AttrTabboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<tabbox>");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<treecols><treecol width=\"80px\" /><treecol /></treecols>");
        }
        
        // sub tr
        final int sum = attrTmpl.getAttrTmplListCount();
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            if (AttrTmplImpl.MODE_TABCONTENT.equals(subAttrTmpl.getDisplayMode())) {
                // tab
                Tab tab = new Tab();
                //重构Uuid用于压力测试, 由于item.getId不一定合法。
                ZKRebuildUuid.rebuildUuidById(tab, 
                    "tb_" + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId() : "_" 
                        + Math.abs(attrTmpl.getAttrId())));
                tab.setLabel(subAttrTmpl == null ? ""
                    : subAttrTmpl.getName() == null ? ""
                        : subAttrTmpl.getName());
                tab.setParent(tabs);
                // tabpanel
                AttrCompFactory.getInst(subAttrTmpl.getDisplayMode()).create(attrExt, subAttrTmpl,
                    tabpanels);
            } else if (AttrTmplImpl.MODE_TABTBAR.equals(subAttrTmpl.getDisplayMode())) { // toolbar
                AttrCompFactory.getInst(subAttrTmpl.getDisplayMode()).create(attrExt, subAttrTmpl,
                    tabbox);
            } else {
                throw new RtManagerException("Tabbox控件下级只允许配置为tab及toolbar类型控件", this.getClass(),
                    "create", "subCompTypeErr");
            }
        }
        
        // log
        if (AttrTabboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("</tabbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
        
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @SuppressWarnings("unchecked")
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        
        // 自身不实例化，取下级数据
        // add by LinQingfeng 2012.07.12
        final List<Component> subCompList = comp.getChildren();
        for (final Component subCompRootBox : subCompList) {
            // 由于没有tabs对应的AttrComp,获取再下一级的控件。
            List<Component> subChilds = subCompRootBox.getChildren();
            for (Component child : subChilds) {
                AttrCompFactory.getInstByComponent(child).setValue(attrExt, subCompRootBox,
                    attrInstList, attrViewMap);
            }
            
        }
        
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @SuppressWarnings("unchecked")
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        
        // 自身不实例化，取下级数据
        // add by LinQingfeng 2012.07.21
        final List<Component> subCompList = comp.getChildren();
        for (final Component subCompRootBox : subCompList) {
            // 由于没有tabs对应的AttrComp,获取再下一级的控件。
            List<Component> subChilds = subCompRootBox.getChildren();
            for (Component child : subChilds) {
                final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(child).getValue(attrExt, subCompRootBox);
                if (subRetList != null) {
                    retList.addAll(subRetList);
                }
            }
            
        }
        
        return retList;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        
        final int sum = attrTmpl.getAttrTmplListCount();

        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @SuppressWarnings("unchecked")
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrTabboxComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).clean(attrExt,
                        subCompRootBox);
                }
          com.xhui.vimarket.zkplus
    }
    
    /**
     * 
     com.xhui.vimarket.zkplus
     * @see com.xhui.vimarket.zkplus.zul.attcom.xhui.vimarket.zkplusBaseComp#exec(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, com.xhui.vimarket.zkplus.zul.attr.ada.AttrExecListener)
     * @author Linqf
     * 2012-7-19 Linqf
     */
    @SuppressWarnings("unchecked")
    @Override
    public void exec(final AttrExt attrExt, final Component comp, AttrExecListener attrExecListener) {
        super.exec(attrExt, comp, attrExecListener);
        // add by LinQingfeng 2012-7-19
        final List<Component> subCompList = comp.getChildren();
        for (final Component subCompRootBox : subCompList) {
            // 由于没有tabs对应的AttrComp,获取再下一级的控件。
            List<Component> subChilds = subCompRootBox.getChildren();
            for (Component child : subChilds) {
                AttrCompFactory.getInstByComponent(child).exec(attrExt,
                    subCompRootBox, attrExecListener);
                // 自身
                attrExecListener.onEvent(subCompRootBox, attrExt);
            }
            
        }
    }
}
